<!-- 文章文磊的添加编辑共用组件 -->
<script setup>
import { ref } from 'vue'
const dialogVisible = ref(false)
import { artEditChannelService, artAddChannelService } from '@/api/article'

// 表单
const formModel = ref({
  cate_name: '',
  cate_alias: '',
  id: ''
})
const rules = {
  cate_name: [
    { required: true, message: '请输入分类名称', trigger: 'blur' },
    { pattern: /^\S{1,15}$/, message: '请输入1-10个非空字符', trigger: 'blur' }
  ],
  cate_alias: [
    { required: true, message: '请输入分类别名', trigger: 'blur' },
    { pattern: /^[a-z1-9A-Z]{1,15}$/, message: '请输入1-15个字母数字', trigger: 'blur' }
  ]
}
const open = (row) => {
  dialogVisible.value = true
  formModel.value = { ...row }
}
defineExpose({
  open
})
const emit = defineEmits(['success'])
const form = ref() //预校验使用
// 点击确认
const onSuccess = async () => {
  await form.value.validate()
  dialogVisible.value = false
  formModel.value.id
    ? await artEditChannelService(formModel.value)
    : await artAddChannelService(formModel.value)
  emit('success')
}
</script>
<template>
  <el-dialog v-model="dialogVisible" :title="formModel.id ? '编辑分类' : '添加分类'" width="500">
    <el-form :model="formModel" :rules="rules" ref="form">
      <el-form-item label="分类名称" prop="cate_name">
        <el-input v-model="formModel.cate_name" placeholder="请填写分类名称" />
      </el-form-item>
      <el-form-item label="分类别名" prop="cate_alias">
        <el-input v-model="formModel.cate_alias" placeholder="请填写分类别名" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onSuccess"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
